package com.caineng.zhaobiao.zbapi.utils.config;

import com.caineng.zhaobiao.zbapi.dao.CaiAddressDao;
import com.caineng.zhaobiao.zbapi.domain.CaiAddress;
import com.caineng.zhaobiao.zbapi.utils.multipleDb.MongodbConfiguration;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目启动时,自动运行
 */
@Slf4j
@Component
public class AppStartRunner implements ApplicationRunner {
    @Autowired
    private CaiAddressDao addressDao;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    @Qualifier(MongodbConfiguration.MONGO_TEMPLATE_SECONDARY)
    private MongoTemplate mongoTemplateSecondary ;

    @Data
    class Address{
        private String provinceName;
        private String cityName;
        private String areaName;

        Address(){

        }

        Address(String provinceName, String cityName, String areaName) {
            this.provinceName = provinceName;
            this.cityName=cityName;
            this.areaName=areaName;
        }
    }

    /**
     * 根据区县,地级市的名称得到省份和地级市
     *
     */
    public static  Map<String,Address> areaNameMap = null;

    public static Map<String,String> shenghuiMap = null ;

    /**
     * 将地区加入内存中
     */
    private void setAreaNameMap(){
        if(areaNameMap==null){
            areaNameMap = new HashMap<String,Address>();
            shenghuiMap = new HashMap<>();
            List<CaiAddress> list = addressDao.findAll();
            for(CaiAddress address : list){
                String provinceName = address.getName();

                List<Map> jsonObjects = address.getCity();
                int i = 0 ;
                for(Map jsonObject : jsonObjects){
                    String cityName = (String)jsonObject.get("name");
                    if(i==0){
                        //log.info(provinceName+"   "+cityName);
                        //一般第一个城市就是省会,除了直辖市
                        shenghuiMap.put(provinceName,cityName);
                    }
                    i++;

                    ArrayList areaJson = (ArrayList)jsonObject.get("county");
                    areaNameMap.put(cityName,new Address(provinceName,cityName,""));
                    //log.info(cityName+":"+provinceName);
                    //地区名称
                    for(Object obj : areaJson){
                        String name = (String) obj;
                        Address addr = new Address();
                        addr.setAreaName(name);
                        addr.setCityName(cityName);
                        addr.setProvinceName(provinceName);
                        areaNameMap.put(name,addr);
                        //log.info(">>"+name+" : "+provinceName+" "+cityName+" "+name);
                    }

                }
            }
        }
    }

    public static List<String> listCompany = new ArrayList<String>();

    public void addCompanyToList(){

       /* System.out.println("开始的内存是:"+RamUsageEstimator.sizeOf(listCompany));
        Query query = new Query();
        Criteria criteria = new Criteria();
        query.fields().include("companyName").include("companyCid");
        List<Map> listAll = mongoTemplate.find(query,Map.class,"cai_quali_company");

        System.out.println("所有的企业查询的内存是:"+RamUsageEstimator.sizeOf(listAll));
        long count = 0 ;
        for(Map map:listAll){

            String companyName = (String)map.get("companyName");
            listCompany.add(companyName);
            count++;
            System.out.println(count);
        }

        System.out.println("运行完成的内存是:"+RamUsageEstimator.sizeOf(listCompany));*/

    }

    @Override
    public void run(ApplicationArguments args) throws InterruptedException {

        //执行一次地区
        setAreaNameMap();

        //企业名称
        addCompanyToList();

    }
}